package com.zdx.yswl.ui.http;

import java.util.Map;

import org.json.JSONObject;

import android.content.Context;
import android.graphics.Bitmap;
import android.util.LruCache;
import android.widget.ImageView;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.ImageLoader.ImageCache;
import com.android.volley.toolbox.ImageLoader.ImageListener;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.NetworkImageView;
import com.android.volley.toolbox.Volley;
import com.zdx.yswl.App;

public class HttpUtilsByVolley {
	
	private String getUrl(){
		return " ";
	}
	
	//常用
	public static void sendGetRequest4Path(Context context,String path,Map<String, Object> params){
		String url = App.getInstance().getHostPath() +	path;
		
//		List<NameValuePair> parameters = new ArrayList<NameValuePair>();
//		for (Map.Entry<String, Object> entry : params.entrySet()) {
//			BasicNameValuePair pair = new BasicNameValuePair(entry.getKey(), entry.getValue().toString());
//			parameters.add(pair);
//		}
		
		sendGetRequest4Url(context,url, new JSONObject(params), null, null);
	}
	
	public static void sendGetRequest4Url(Context context ,String url,JSONObject json,Listener<JSONObject> listener,ErrorListener errorListener){
		RequestQueue requestQueue = Volley.newRequestQueue(context);
		JsonObjectRequest request = new JsonObjectRequest( //
				Request.Method.GET, url, json,listener,errorListener);
		requestQueue.add(request);

	}
	
	
	public static void sendPostRequest4Url(Context context ,String url,JSONObject json,Listener<JSONObject> listener,ErrorListener errorListener){
		RequestQueue requestQueue = Volley.newRequestQueue(context);
		JsonObjectRequest request = new JsonObjectRequest( //
				Request.Method.POST, url, json,listener,errorListener);
		requestQueue.add(request);

	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	/**
	 * @param context
	 * @param imagUrl 
	 * @param iv  ImageView实例  
	 * @param defaultImageResId  默认的图片resource id  
	 * @param errorImageResId   请求失败时候的资源id，可以指定为0  
	 */
	public static void loadImage4Url(Context context,String imagUrl,ImageView iv,int defaultImageResId, int errorImageResId){
		RequestQueue request = Volley.newRequestQueue(context);
		final LruCache<String , Bitmap> lurcache = new LruCache<String, Bitmap>(20);
		ImageCache imageCache = new ImageCache() {
			@Override
			public void putBitmap(String url, Bitmap bitmap) {
				lurcache.put(url, bitmap);
			}
			
			@Override
			public Bitmap getBitmap(String url) {
				return lurcache.get(url);
			}
		};
		
		ImageLoader imageLoad = new ImageLoader(request, imageCache);
		ImageListener listener = imageLoad.getImageListener(iv, defaultImageResId, errorImageResId);
		imageLoad.get(imagUrl, listener);
	}
	
	/**
	 * 用 volley 的图片控件 com.android.volley.toolbox.NetworkImageView
	 * @param context
	 */
	public void NetWorkImageViewVolley(Context context,String imagUrl,NetworkImageView imagView){
		RequestQueue request = Volley.newRequestQueue(context);
		final LruCache<String , Bitmap> lurcache = new LruCache<String, Bitmap>(20);
		ImageCache iamgeCache = new ImageCache() {
			
			@Override
			public void putBitmap(String url, Bitmap bitmap) {
				lurcache.put(url, bitmap);
			}
			@Override
			public Bitmap getBitmap(String url) {
				return lurcache.get(url);
			}
		};
		ImageLoader loader = new ImageLoader(request, iamgeCache);
		imagView.setTag("url");
		imagView.setImageUrl(imagUrl,loader);
		
	}
	
	
}
